package com.jiuqian.demo.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jiuqian.common.core.Result;
import com.jiuqian.common.core.base.BaseController;
import com.jiuqian.common.core.config.RedisRepository;
import com.jiuqian.demo.model.Area;
import com.jiuqian.demo.service.AreaService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;

@RestController
@RequestMapping({ "/area" })
@Api(value = "area", description = "区域(省/市/区/街道)")
public class AreaController extends BaseController {
	@Autowired
	private AreaService areaService;

	@Autowired
	private RedisRepository redisRepository;

	@GetMapping({ "/provinces" })
	@ApiOperation(value = "获取全国省", httpMethod = "GET")
	@ApiResponse(code = 0, message = "Ok", response = Result.class)
	public Result<List<Area>> loadProvinceArea() {
		List<Area> areaList = this.areaService.findProvinces();
		return response(areaList);
	}

	@GetMapping({ "/province/{provinceId}/citys" })
	@ApiOperation(value = "获取省下的市", httpMethod = "GET")
	@ApiImplicitParam(name = "provinceId", value = "省份ID", required = true, dataType = "int", paramType = "path")
	@ApiResponse(code = 200, message = "Ok", response = Result.class)
	public Result<List<Area>> loadCity(@PathVariable(value = "provinceId", required = true) Integer provinceId) {
		List<Area> areaList = this.areaService.findCityByProvinceId(provinceId);
		return response(areaList);
	}

	@GetMapping({ "/city/{cityId}/regions" })
	@ApiOperation(value = "获取市下的区/县", httpMethod = "GET")
	@ApiImplicitParam(name = "cityId", value = "市ID", required = true, dataType = "int", paramType = "path")
	@ApiResponse(code = 200, message = "Ok", response = Result.class)
	public Result<List<Area>> loadRegion(@PathVariable(value = "cityId", required = true) Integer cityId) {
		List<Area> areaList = this.areaService.findRegionByCityId(cityId);
		return response(areaList);
	}

	@GetMapping({ "/region/{regionId}/streets" })
	@ApiOperation(value = "获取区/县下的街道", httpMethod = "GET")
	@ApiImplicitParam(name = "regionId", value = "县/区ID", required = true, dataType = "int", paramType = "path")
	@ApiResponse(code = 200, message = "Ok", response = Result.class)
	public Result<List<Area>> loadStreet(@PathVariable(value = "regionId", required = true) Integer regionId) {
		List<Area> areaList = this.areaService.findStreetByRegionId(regionId);
		return response(areaList);
	}

	@GetMapping({ "/{parentId}" })
	@ApiOperation(value = "根据父ID获取子地区列表", httpMethod = "GET")
	@ApiImplicitParam(name = "parentId", value = "父ID（id为空则默认查询所有省）", required = false, dataType = "int", paramType = "path")
	@ApiResponse(code = 200, message = "Ok", response = Result.class)
	public Result<List<Area>> loadRn(@PathVariable(value = "parentId", required = false) Integer parentId) {
		List<Area> areaList = null;
		if ((parentId != null) && (parentId.intValue() > 0)) {
			areaList = this.areaService.finByParentId(parentId);
		} else {
			areaList = this.areaService.findProvinces();
		}
		return response(areaList);
	}
}
